home *** CD-ROM | disk | FTP | other *** search
/ Game.EXE 2001 January / Game.EXE_01_2001.iso / demos / Blade of Darkness / data1.cab / Program_Executable_Files / Lib / NetMisc.py < prev    next >
Encoding:
Text File  |  2000-11-16  |  9.2 KB  |  463 lines

  1. ######################### Configuracion de red ##############################
  2. try:
  3.     execfile('../../Config/NetCfg.py')
  4. except:
  5.     TCP           = 1
  6.     CHARTYPE      = "Knight"
  7.     MODEL         = 0
  8.     HANDICAP      = 4
  9.     PLAYERNAME    = "BLADE"
  10.     ARENANAME     = "ARENA"
  11.     ARENAHANDICAP = 4
  12.     POWERUPS      = 1
  13.     MAXPLAYERS    = 5
  14.     SELMAPS       = []
  15.     ACTUALMAP     = 0
  16.     IP            = ""
  17.     FRAGLIMIT     = 6
  18.     NETPPS        = 0
  19. ######################### Configuracion de red ##############################
  20.  
  21. import netwidgets
  22. import MenuText
  23. import MenuWidget
  24. import netgame
  25. import Bladex
  26. import string
  27. import os
  28.  
  29. Caracteres = {     "Knight"     :[0,"Knight_N"
  30.                     ,"Knight_N"
  31.                     ,"KgtSkin1"
  32.                     ,"KgtSkin2"
  33.                 
  34.         ],"Barbarian" :[1,"Barbarian_N"
  35.                     ,"Barbarian_N"
  36.                     ,"BarSkin1"
  37.                     ,"BarSkin2"
  38.                 
  39.         ],"Dwarf"     :[2,"Dwarf_N"
  40.                     ,"Dwarf_N"
  41.                     ,"DwfSkin1"
  42.                     ,"DwfSkin2"
  43.         ],"Amazon"    :[3,"Amazon_N"
  44.                         ,"Amazon_N"
  45.                         ,"AmzSkin1"
  46.                         ,"AmzSkin2"
  47.         ]}
  48.  
  49. CharBitmaps =     (
  50.             ("Knight_N"    , "../../Data/net/Knight_N.BMP"   ),
  51.             ("KgtSkin1"    , "../../Data/net/KgtSkin1.BMP"   ),
  52.             ("KgtSkin2"    , "../../Data/net/KgtSkin2.BMP"   ),
  53.             ("Barbarian_N" , "../../Data/net/Barbarian.BMP"  ),
  54.             ("BarSkin1"    , "../../Data/net/BarSkin1.BMP"   ),
  55.             ("BarSkin2"    , "../../Data/net/BarSkin2.BMP"   ),
  56.             ("Dwarf_N"     , "../../Data/net/Dwarf_N.BMP"    ),
  57.             ("DwfSkin1"    , "../../Data/net/DwfSkin1.BMP"   ),
  58.             ("DwfSkin2"    , "../../Data/net/DwfSkin2.BMP"   ),
  59.             ("Amazon_N"    , "../../Data/net/Amazon_L.BMP"   ),
  60.             ("AmzSkin1"    , "../../Data/net/AmzSkin1.BMP"   ),
  61.             ("AmzSkin2"    , "../../Data/net/AmzSkin2.BMP"   ),
  62.         )
  63.  
  64. MapsBitmaps =   []
  65.  
  66. def ActualizeMapsBitmaps():
  67.     global MapsBitmaps
  68.     
  69.     while len(MapsBitmaps)!=0:
  70.         MapsBitmaps.remove(MapsBitmaps[0])
  71.     
  72.     dirlist = os.listdir("..")
  73.     for dirname in dirlist:
  74.         try:
  75.             f = open("../"+dirname+"/Server.py")
  76.         except:
  77.             continue        
  78.         print dirname
  79.         cadena = f.readline()
  80.         f.close()
  81.         
  82.         if cadena[0] == "#":
  83.             cadena = string.strip(cadena[1:len(cadena)-1])
  84.         else:
  85.             cadena = dirname
  86.         try:
  87.             escrinchot = "../"+dirname+"/shoot.bmp"
  88.             f = open(escrinchot)
  89.             f.close()
  90.             
  91.         except:
  92.             escrinchot = "../../Data/net/Logo.BMP"
  93.  
  94.         MapsBitmaps.append(escrinchot,cadena,dirname)
  95.     return MapsBitmaps
  96.         
  97.         
  98.             
  99.  
  100. def SetPPS(option):
  101.     global NETPPS
  102.     
  103.     if option == "Variable":
  104.         NETPPS = 0
  105.     else:
  106.         NETPPS = string.atoi(option)
  107.  
  108.  
  109. def GetPPS():
  110.     global NETPPS
  111.     
  112.     return int(NETPPS/10)
  113.     
  114.  
  115. def SetProtocol(option):
  116.     global TCP
  117.     if option=="TCP/IP (Internet)":
  118.         TCP = 1
  119.     else:
  120.         TCP = 0
  121.  
  122.     
  123. def GetProtocol():
  124.     global TCP
  125.     if TCP:
  126.         return  1
  127.     else:
  128.         return  0
  129.  
  130. def SetCharType(option):
  131.     global CHARTYPE
  132.     global MODEL
  133.     
  134.     CHARTYPE = option
  135.     MODEL    = 0
  136.     netwidgets.ChangePlayer(Caracteres[CHARTYPE][MODEL+2])
  137.     
  138.  
  139. def GetCharType():
  140.     global CHARTYPE
  141.     
  142.     return Caracteres[CHARTYPE][0]
  143.  
  144. def SetHandicap(option):
  145.     global HANDICAP
  146.     
  147.     HANDICAP = option
  148.     
  149. def GetHandicap():
  150.     global HANDICAP
  151.     
  152.     return HANDICAP
  153.     
  154. def NextSkin(menu_class):
  155.     global MODEL
  156.     global CHARTYPE
  157.     
  158.     size = len(Caracteres[CHARTYPE])-2
  159.     MODEL = MODEL + 1
  160.     if MODEL>=size:
  161.         MODEL = 0
  162.     netwidgets.ChangePlayer(Caracteres[CHARTYPE][MODEL+2])
  163.  
  164. def SetPowerUp(option):
  165.     global POWERUPS
  166.     if option=="Yes":
  167.         POWERUPS = 0
  168.     else:
  169.         POWERUPS = 1
  170.         
  171. def GetPowerUp():
  172.     global POWERUPS
  173.     return POWERUPS
  174.  
  175. def SetMaxPlayers(option):
  176.     global MAXPLAYERS
  177.     
  178.     MAXPLAYERS = option
  179.     
  180. def GetMaxPlayers():
  181.     global MAXPLAYERS
  182.     
  183.     return MAXPLAYERS
  184.  
  185. def SetArenaHandicap(option):
  186.     global ARENAHANDICAP
  187.     
  188.     ARENAHANDICAP = option
  189.     
  190. def GetArenaHandicap():
  191.     global ARENAHANDICAP
  192.     
  193.     return ARENAHANDICAP
  194.  
  195. def SetArenaMaps(option):    
  196.     global SELMAPS
  197.     SELMAPS = option
  198.     
  199. def GetArenaMaps():
  200.     global SELMAPS
  201.     return  SELMAPS
  202.  
  203. def GetCharStatus():
  204.     global CHARTYPE
  205.     global MODEL
  206.     return Caracteres[CHARTYPE][MODEL+2]
  207.  
  208. def GetArenaName():
  209.     global ARENANAME
  210.     return  ARENANAME
  211.  
  212. def GetPlayerName():
  213.     global PLAYERNAME
  214.     return  PLAYERNAME
  215.  
  216. def GetIp():
  217.     global IP
  218.     global TCP
  219.     
  220.     if TCP:
  221.         return IP
  222.     else:
  223.         return "LOCAL"
  224.     
  225.  
  226. def SetArenaName(n):
  227.     global ARENANAME
  228.     ARENANAME = n
  229.  
  230. def SetPlayerName(n):
  231.     global PLAYERNAME
  232.     PLAYERNAME = n
  233.  
  234. def SetIp(n):
  235.     global IP
  236.     global TCP
  237.     
  238.     if TCP :
  239.         IP = n
  240.  
  241. def IpLooked():
  242.     global TCP
  243.     return not TCP
  244.  
  245. def SetFragLimit(option):
  246.     global FRAGLIMIT
  247.     
  248.     FRAGLIMIT = option
  249.     
  250. def GetFragLimit():
  251.     global FRAGLIMIT
  252.     
  253.     return FRAGLIMIT
  254.  
  255.  
  256. def JoinFunc(Name,NumPlayers,MaxPlayers,Idx):
  257.     global PLAYERNAME
  258.  
  259.     netgame.SetPPS(0)
  260.     print Name,NumPlayers,MaxPlayers,Idx,"Loading..."
  261.     SaveConfiguration()
  262.     netgame.JoinSession(Idx,PLAYERNAME)
  263.     Bladex.LoadLevel(SELMAPS[0])
  264.  
  265. def RefreshFunc(Name,NumPlayers,MaxPlayers,Idx):
  266.     BrowseServers(0)
  267.  
  268. def NullFunc(Name,NumPlayers,MaxPlayers,Idx):
  269.     pass
  270.     
  271. def BrowseServers(menu_class):
  272.     print "searching..."
  273.     global TCP
  274.     global IP
  275.     
  276.     lista = []
  277.     netgame.RestartNet()
  278.     if TCP:
  279.         netgame.BrowseSessions(IP)
  280.     else:
  281.         netgame.BrowseSessions("[ipx]")
  282.     i = 0
  283.     Srv = netgame.GetBrowseResult(i)
  284.     
  285.     while Srv != None:
  286.         lista.append({    "Name"        : Srv[0],
  287.                 "NumPlayers"    : Srv[1],
  288.                 "MaxPlayers"    : Srv[2],
  289.                 "Idx"        : i,
  290.                 "Join"        : JoinFunc,
  291.                 "Kind"        : netwidgets.B_ServerLabel#MenuWidget.B_MenuItemTextNoFX#
  292.                 })
  293.         i=i+1
  294.         Srv = netgame.GetBrowseResult(i)
  295.  
  296.     if len(lista)>=1:
  297.         lista.append({    "Name"        : MenuText.GetMenuText("< REFRESH >"),
  298.                 "NumPlayers"    : 0,
  299.                 "MaxPlayers"    : 0,
  300.                 "Idx"        : 0,
  301.                 "Join"        : RefreshFunc,
  302.                 "Kind"        : netwidgets.B_ServerLabel#MenuWidget.B_MenuItemTextNoFX#
  303.                 })
  304.     else:
  305.         lista.append({    "Name"        : MenuText.GetMenuText("< No server found >"),
  306.                 "NumPlayers"    : 0,
  307.                 "MaxPlayers"    : 0,
  308.                 "Idx"        : 0,
  309.                 "Join"        : NullFunc,
  310.                 "Kind"        : netwidgets.B_ServerLabel#MenuWidget.B_MenuItemTextNoFX#
  311.                 })
  312.     
  313.     
  314.     netwidgets.LaListaDeServidores.SetList(lista)
  315.     if len(lista)>=1:
  316.         import Menu
  317.         Menu._MainMenu.MenuNextItem()
  318.     print "Ready!"
  319.  
  320. MainMenu = 0
  321.  
  322. def StartServer(menu_class):
  323.     global ARENANAME
  324.     global PLAYERNAME
  325.     global MAXPLAYERS
  326.     global TCP
  327.     global SELMAPS
  328.     global ACTUALMAP
  329.     global MainMenu
  330.     global NETPPS
  331.     
  332.     if len(SELMAPS)==0:
  333.         return
  334.         
  335.     MainMenu.DeActivateMenuItem()
  336.     netgame.RestartNet()
  337.     print "start server : ", netgame.StartServer(ARENANAME,PLAYERNAME,MAXPLAYERS,TCP)
  338.     ACTUALMAP = 0
  339.     SaveConfiguration()
  340.     print "Loading ",SELMAPS[ACTUALMAP]
  341.     Bladex.LoadLevel(SELMAPS[ACTUALMAP])
  342.     netgame.SetPPS(NETPPS)
  343.     
  344.     return
  345.  
  346. def ServerNextLevel():
  347.     global ACTUALMAP
  348.     global SELMAPS
  349.     ACTUALMAP = (ACTUALMAP+1)%len(SELMAPS)
  350.     SaveConfiguration()
  351.     netgame.ServerChangeLevel(SELMAPS[ACTUALMAP])
  352.     
  353.     
  354. def SavePlayerConfiguration(xx=0):
  355.     SaveConfiguration()
  356.     MainMenu.DeActivateMenuItem()
  357.  
  358. def SaveConfiguration(xx=0):
  359.     global TCP           
  360.     global CHARTYPE      
  361.     global MODEL         
  362.     global HANDICAP      
  363.     global PLAYERNAME    
  364.     global ARENANAME     
  365.     global ARENAHANDICAP 
  366.     global POWERUPS      
  367.     global MAXPLAYERS    
  368.     global SELMAPS       
  369.     global ACTUALMAP     
  370.     global IP            
  371.     global FRAGLIMIT     
  372.     global NETPPS        
  373.     
  374.     cfgfile=open('../../Config/NetCfg.py','w')
  375.     cfgfile.write('\n# The sacred file of blade is here!\n\n\n')
  376.     cfgfile.write("TCP           = "+`TCP`+'\n')
  377.     cfgfile.write("CHARTYPE      = "+'"'+CHARTYPE+'"'+'\n')
  378.     cfgfile.write("MODEL         = "+`MODEL`+'\n')
  379.     cfgfile.write("HANDICAP      = "+`HANDICAP`+'\n')
  380.     cfgfile.write("PLAYERNAME    = "+'"'+PLAYERNAME+'"'+'\n')
  381.     cfgfile.write("ARENANAME     = "+'"'+ARENANAME+'"'+'\n')
  382.     cfgfile.write("ARENAHANDICAP = "+`ARENAHANDICAP`+'\n')
  383.     cfgfile.write("POWERUPS      = "+`POWERUPS`+'\n')
  384.     cfgfile.write("MAXPLAYERS    = "+`MAXPLAYERS`+'\n')
  385.     cfgfile.write("SELMAPS       = "+`SELMAPS`+'\n')
  386.     cfgfile.write("ACTUALMAP     = "+`ACTUALMAP`+'\n')
  387.     cfgfile.write("IP            = "+'"'+IP+'"'+'\n')
  388.     cfgfile.write("FRAGLIMIT     = "+`FRAGLIMIT`+'\n')
  389.     cfgfile.write("NETPPS        = "+`NETPPS`+'\n')
  390.     
  391.     
  392.     if len(SELMAPS)==0:
  393.         SELMAPS = [MapsBitmaps[0][2]]
  394.         
  395.     netgame.SetLocalOptions(PLAYERNAME,Caracteres[CHARTYPE][1],Caracteres[CHARTYPE][MODEL+2],`HANDICAP`,SELMAPS[ACTUALMAP])
  396.     
  397.     cfgfile.write('\n# God bless your way, little warrior.\n\n\n')
  398.     cfgfile.close()
  399.     
  400. def Disconnect(xx=0):
  401.     netgame.RestartNet()
  402.     Bladex.LoadLevel("Casa")
  403.     
  404.     
  405. LReadUserString = []
  406.  
  407. def ReadUserString(id,type,cad):
  408.     
  409.     print "Lee Cadena"
  410.     
  411.     global LReadUserString
  412.     for f in LReadUserString:
  413.         f(id,type,cad)
  414.  
  415.  
  416. LOnStartRoundFunc = []
  417.  
  418. def OnStartRoundFunc():
  419.     
  420.     print "Inicia Round"
  421.     
  422.     global LOnStartRoundFunc
  423.     for f in LOnStartRoundFunc:
  424.         f()
  425.  
  426.  
  427.  
  428. LOnEndRoundFunc = []
  429.  
  430. def OnEndRoundFunc():
  431.     print "Finaliza Round"
  432.     
  433.     global LOnEndRoundFunc
  434.     for f in LOnEndRoundFunc:
  435.         f()
  436.  
  437. LOnDamageFunc = []
  438.  
  439. def OnDamageFunc(entity):
  440.     global LOnDamageFunc
  441.     for f in LOnDamageFunc:
  442.         if f(entity):
  443.             return 1
  444.     return 0
  445.     
  446.  
  447. def SetMainMenu(_MainMenu_):
  448.     global MainMenu
  449.     
  450.     MainMenu = _MainMenu_
  451.     
  452.     
  453.     
  454. def GetClientPlayerId(xx):
  455.     if netgame.GetNetState() == 2:
  456.         if(xx == "Player1"):
  457.             return "PlayerX"
  458.         elif(xx == netgame.GetClientId()):
  459.             return "Player1"
  460.         else:
  461.             return xx
  462.     else:
  463.         return xx